<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发生时间">
        <el-date-picker
          v-model="daterangeWerrorTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="werrorNick">
        <el-input
          v-model="queryParams.werrorNick"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sl:werror:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sl:werror:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sl:werror:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sl:werror:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="werrorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发生时间" align="center" prop="werrorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.werrorTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人物" align="center" prop="werrorNick" />
      <el-table-column label="地点" align="center" prop="werrorAddress" />
      <el-table-column label="描述" align="center" prop="werrorContent" />
      <el-table-column label="处罚金额" align="center" prop="werrorFine" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <el-button
            v-if="scope.row.mpayId!=''"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:werror:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.mpayId!=''"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:werror:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作绩效对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="720px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发生时间" prop="werrorTime">
          <el-date-picker clearable
            v-model="form.werrorTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="人物" prop="werrorNick">
          <el-input v-model="form.werrorNick" readonly="true" placeholder="请选择人物" >
            <el-button slot="append" @click="handleUser2Select" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <UserSingleSelect ref="user2Select" @onSelected="onUser2Selected"></UserSingleSelect>
        <el-form-item label="地点" prop="werrorAddress">
          <el-input v-model="form.werrorAddress" placeholder="请输入地点" />
        </el-form-item>
        <el-form-item label="处罚金额" prop="werrorFine">
          <el-input v-model="form.werrorFine" class="def-input-number" type="number" placeholder="请输入处罚金额" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input rows="5" v-model="form.werrorContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWerror, getWerror, delWerror, addWerror, updateWerror } from "@/api/sl/werror";
import UserSingleSelect from "@/components/userSelect/single.vue"

export default {
  name: "Werror",
  components: {UserSingleSelect},
  data() {
    let watermarkContentVaild = (rule, value, callback) => {
      if (value == null || value === "") {
        callback();
      } else {
        let length = value.length
        if (length > 7) {
          callback(new Error("输入不能超过7位"))
        } else {
          callback();
        }
      }
    }
    
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作绩效表格数据
      werrorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 车间0行政1时间范围
      daterangeWerrorTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        werrorTime: null,
        werrorNick: null,
        werrorUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        werrorTime: [
          { required: true, message: "时间不能为空", trigger: "blur" }
        ],
        werrorContent: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        werrorNick: [
          { required: true, message: "人物不能为空", trigger: "blur" }
        ],
        werrorFine: [
          { required: true, message: "处罚金额不能为空", trigger: "blur" },
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工作绩效列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeWerrorTime && '' != this.daterangeWerrorTime) {
        this.queryParams.params["beginWerrorTime"] = this.daterangeWerrorTime[0];
        this.queryParams.params["endWerrorTime"] = this.daterangeWerrorTime[1];
      }
      listWerror(this.queryParams).then(response => {
        this.werrorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        werrorId: null,
        werrorTime: null,
        werrorAddress: null,
        werrorContent: null,
        werrorNick: null,
        werrorUser: null,
        werrorFine: null,
        submitTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDone: null,
        doneTime: null,
        werrorLevel: null,
        werrorType: null,
        mpayId: null,
        workOfc: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeWerrorTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.werrorId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作绩效";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const werrorId = row.werrorId || this.ids
      getWerror(werrorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作绩效";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.werrorId != null) {
            updateWerror(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWerror(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const werrorIds = row.werrorId || this.ids;
      this.$modal.confirm('是否确认删除工作绩效编号为"' + werrorIds + '"的数据项？').then(function() {
        return delWerror(werrorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/werror/export', {
        ...this.queryParams
      }, `werror_${new Date().getTime()}.xlsx`)
    },
    //点击人员选择按钮
    handleUser2Select(){
        this.$refs.user2Select.showFlag = true;
    },
    //人员选择返回
    onUser2Selected(row){
        this.form.werrorUser = row.userName;
        this.form.werrorNick = row.nickName;
    },
  }
};
</script>
